package com.yh.hljapp.gddb.gbmanager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.yh.hljapp.gddb.gdbean.DaoMaster;
import com.yh.hljapp.gddb.gdbean.DaoSession;
import com.yh.hljapp.gddb.gdbean.MatBean;
import com.yh.hljapp.gddb.gdbean.MatBeanDao;

import java.util.ArrayList;
import java.util.List;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：MatManager
 * <p>
 * 创建时间：2017.05.23.15:05
 * *****************************
 */
public class MatManager {
    private final static String dbName = "fb_db";
    private static MatManager mInstance;
    private DaoMaster.DevOpenHelper openHelper;
    private Context context;
    private final DaoSession mDaoSession;

    public MatManager(Context context) {
        this.context = context;
        openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        mDaoSession = daoMaster.newSession();
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static MatManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (MatManager.class) {
                if (mInstance == null) {
                    mInstance = new MatManager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     */
    private SQLiteDatabase getReadableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     */
    private SQLiteDatabase getWritableDatabase() {
        if (openHelper == null) {
            openHelper = new DaoMaster.DevOpenHelper(context, dbName, null);
        }
        SQLiteDatabase db = openHelper.getWritableDatabase();
        return db;
    }

    //插入一个
    public void insertMat(MatBean itemListBean) {
        MatBeanDao matBeanDao = mDaoSession.getMatBeanDao();
        long insert = matBeanDao.insert(itemListBean);
    }

    //插入集合
    public boolean insertMatList(List<MatBean> mbs) {
        MatBeanDao matBeanDao = mDaoSession.getMatBeanDao();
        try {
            matBeanDao.deleteAll();
            matBeanDao.insertOrReplaceInTx(mbs);
        } catch (Exception e) {
            return false;
        }
        return true;

    }

    public int getMatCount() {
        MatBeanDao matBeanDao = mDaoSession.getMatBeanDao();
        List<MatBean> matBeen = matBeanDao.loadAll();
        return matBeen.size();
    }

    //判断扫描的出库单是否存在
    public boolean isMatExist(String barcode, String type) {
        MatBeanDao matBeanDao = mDaoSession.getMatBeanDao();
//        MatBean unique = matBeanDao.queryBuilder().where(MatBeanDao.Properties.Barcode.eq(barcode),MatBeanDao.Properties.Ftype.eq(type)).unique();
//        if (unique == null) {
//            return false;
//        } else {
//            return true;
//        }
        List<MatBean> list = matBeanDao.queryBuilder().where(MatBeanDao.Properties.Barcode.eq(barcode), MatBeanDao.Properties.Ftype.eq(type)).list();
        if (list != null && list.size() > 0) {
            return true;
        } else {
            return false;
        }
    }

    //获得该条码对应多个
    public ArrayList<MatBean> getMulMat(String barcode, String type){
        MatBeanDao matBeanDao = mDaoSession.getMatBeanDao();
        ArrayList<MatBean> list = (ArrayList<MatBean>) matBeanDao.queryBuilder().where(MatBeanDao.Properties.Barcode.eq(barcode), MatBeanDao.Properties.Ftype.eq(type)).list();
        return list;
    }


    //判断该条码是否对应多个  只有size大于1返回true
    public boolean isMulMat(String barcode, String type){
        MatBeanDao matBeanDao = mDaoSession.getMatBeanDao();
        ArrayList<MatBean> list = (ArrayList<MatBean>) matBeanDao.queryBuilder().where(MatBeanDao.Properties.Barcode.eq(barcode), MatBeanDao.Properties.Ftype.eq(type)).list();
        if (list!=null&&list.size()>0){
            if (list.size()>1){
                return true;
            }else if (1==list.size()){
                return false;
            }
        }else{
            return false;
        }
        return false;
    }


    /**
     * 通过物料唯一的id拿到物料信息
     */
    public MatBean getMatById(String id){
        MatBean unique = mDaoSession.getMatBeanDao().queryBuilder().where(MatBeanDao.Properties.Id.eq(id)).unique();
        return unique;
    }


    //通过id拿到赠品基础积分
    public String getCreaditById(String barcode, String type) {
        MatBeanDao matBeanDao = mDaoSession.getMatBeanDao();
        MatBean unique = matBeanDao.queryBuilder().where(MatBeanDao.Properties.Barcode.eq(barcode), MatBeanDao.Properties.Ftype.eq(type)).unique();
        if (unique == null) {
            return "";
        } else {
            return unique.getCredit();
        }
    }

    //判断扫描的出库单是否存在
    public MatBean getMatById(String barcode, String type) {
        MatBeanDao matBeanDao = mDaoSession.getMatBeanDao();
        MatBean unique = matBeanDao.queryBuilder().where(MatBeanDao.Properties.Barcode.eq(barcode), MatBeanDao.Properties.Ftype.eq(type)).unique();
        return unique;
    }

    //根据barcode返回唯一的商品
    public MatBean getBackMatByBarCode(String barcode) {
        MatBean unique = mDaoSession.getMatBeanDao().queryBuilder().where(MatBeanDao.Properties.Barcode.eq(barcode), MatBeanDao.Properties.Ftype.eq("0")).unique();
        return unique;
    }

}
